import ConfigParser, platform, os, sys

def which(program):
    import os
    def is_exe(fpath):
        return os.path.exists(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
        if is_exe(program):
            return program
    else:
        for path in os.environ["PATH"].split(os.pathsep):
            exe_file = os.path.join(path, program)
            if is_exe(exe_file):
                return exe_file

    return None

config = ConfigParser.ConfigParser()
## eliminam vechiul fisier de configurare, daca acesta nu se afla in directorul 'pymaxe' din HOME
if os.environ.get("APPDATA"):
	if not os.path.isdir(os.environ.get("APPDATA") + '\\pymaxe'):
		try:
			os.remove(os.environ.get("APPDATA") + '\\pymaxe')
		except:
			pass
	try:
		os.mkdir(os.environ.get("APPDATA") + '\\pymaxe')
	except:
		pass
	try:
		os.mkdir(os.environ.get("APPDATA") + '\\pymaxe\\plugins')
	except:
		pass
	cfgfile = os.environ.get("APPDATA") + '\\pymaxe\\pymaxe.ini';
else:
	if not os.path.isdir(os.getenv("HOME") + '/.pymaxe'):
		try:
			os.remove(os.getenv("HOME") + '/.pymaxe')
		except:
			pass
	try:
		os.mkdir(os.getenv("HOME") + '/.pymaxe')
	except:
		pass
	try:
		os.mkdir(os.getenv("HOME") + '/.pymaxe/plugins')
	except:
		pass
	cfgfile = os.getenv("HOME") + '/.pymaxe/pymaxe.cfg';
if not os.path.exists(cfgfile):
	config.add_section('General')
	if os.environ.get("APPDATA"):
		config.set('General', 'saveIn', os.environ.get("USERPROFILE") + '\\Desktop')
	else:
		config.set('General', 'saveIn', os.environ.get("HOME") + '/Desktop')
	config.set('General', 'noConvert', True)
	config.set('General', 'autoOpen', False)
	config.set('General', 'splashScreen', True)
	if os.environ.get("APPDATA"):
		config.set('General', 'ffmpegAudio', '-i INPUT -acodec libmp3lame -ab 128k OUTPUT')
		config.set('General', 'ffmpegVideo', '-i INPUT -vcodec msmpeg4 -qmax 8 -acodec libmp3lame OUTPUT')	
	else:
		config.set('General', 'ffmpegAudio', '-i INPUT -acodec libmp3lame -ab 128k OUTPUT')
		config.set('General', 'ffmpegVideo', '-i INPUT -vcodec msmpeg4 -qmax 8 -acodec libmp3lame -ab 128k OUTPUT')
	config.set('General', 'checkUpdates', True)
	config.set('General', 'updatePlugins', True)
	config.add_section('Plugins')
	
	pFiles = os.listdir('plugins')
	sys.path.append('plugins')
	plist = []
	for fname in pFiles:
		plist.append(fname)
	if 'ThemeConfig' in plist:
		plist.pop(plist.index('ThemeConfig'))
	if 'YouTube' in plist:
		plist.pop(plist.index('YouTube'))
	plugins = ';'.join(plist)
	config.set('Plugins', 'plugins', plugins)

	MediaPlayer = ''
	if os.environ.get("APPDATA"):
		try:
			pf = os.environ.get("ProgramFiles")
		except:
			pf = 'C:/Program Files'
		players = [pf + '/Webteh/BSplayer/bsplayer.exe', pf + '/VideoLAN/VLC/vlc.exe', pf + '/SMPlayer/smplayer.exe']

		for x in players:
			if os.path.exists(x):
				MediaPlayer = x
				break
	else:
		players = ['mplayer', 'totem', 'vlc', 'kaffeine', 'smplayer', 'xine', 'dragon']
		for x in players:
			if which(x):
				MediaPlayer = which(x)
				break
	config.set('General', 'streamPlayer', MediaPlayer)
		
	fHandler = open(cfgfile, "wb")
	config.write(fHandler)
	fHandler.close()
	config.read(cfgfile)
else:
	config.read(cfgfile)
	
try:
	if config.get('General', 'ffmpegAudio') == '-i INPUT -acodec mp3 -ab 128 OUTPUT':
		config.set('General', 'ffmpegAudio', '-i INPUT -acodec libmp3lame -ab 128k OUTPUT')
	if config.get('General', 'ffmpegAudio') == '-i INPUT -acodec mp3 -ab 128k OUTPUT':
		config.set('General', 'ffmpegAudio', '-i INPUT -acodec libmp3lame -ab 128k OUTPUT')
	elif config.get('General', 'ffmpegAudio') == '-i INPUT -acodec libmp3lame -ab 128 OUTPUT':
		config.set('General', 'ffmpegAudio',  '-i INPUT -acodec libmp3lame -ab 128k OUTPUT')
	fHandler = open(cfgfile, "wb")
	config.write(fHandler)
	fHandler.close()
except:
	pass
	
#try:
#	if config.getboolean('Plugins', 'triluliluimage') == True:
#		config.set('Plugins', 'triluliluimage',  False)
#	if config.getboolean('Plugins', 'triluliluvideo') == True:
#		config.set('Plugins', 'triluliluvideo',  False)
#	fHandler = open(cfgfile, "wb")
#	config.write(fHandler)
#	fHandler.close()
#except:
#	pass

